﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Collections;

namespace SPMS.Utility
{

    /// <summary>
    /// 日期处理的Utility方法
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [FuQi]   10/9/2008 11:46 AM    Created
    /// </history>
    public static class DateUtility
    {
        private static Hashtable  _japaneseYearNamePair = null;

        private static Hashtable JapaneseYearNamePair()
        {
            if (DateUtility._japaneseYearNamePair == null)
            {
                DateUtility._japaneseYearNamePair = DateUtility.BuildJapaneseYearNamePair();
            }
            return DateUtility._japaneseYearNamePair;
        }

        private static Hashtable BuildJapaneseYearNamePair()
        {

            Hashtable japaneseYearNamePair = new Hashtable();

            japaneseYearNamePair.Add("平成", "H");
            japaneseYearNamePair.Add("昭和", "S");
            japaneseYearNamePair.Add("大正", "T");
            japaneseYearNamePair.Add("明治", "M");

            return japaneseYearNamePair;

        }


        /// <summary>
        /// 根据formatString格式化日期，各产品的日期格式应该统一
        /// </summary>
        /// <param name="dateValue">需要转换的日期型</param>
        /// <param name="formatString">转化的格式化字符串</param>
        /// <returns>格式化后的日期,字符串类型</returns>
        /// <remarks></remarks>
        /// <history>
        /// [CaiShengRong]   2008/11/25 15:10    Created
        /// </history>
        public static string FormatDate(this System.DateTime dateValue, string formatString)
        {
            return FormatDate2(dateValue, formatString);
        }

        /// <summary>
        /// 根据formatString格式化日期，各产品的日期格式应该统一
        /// </summary>
        /// <param name="dateValue">需要转换的日期型</param>
        /// <param name="formatString">转化的格式化字符串</param>
        /// <returns>格式化后的日期,字符串类型</returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   10/9/2008 1:18 PM    Created
        /// </history>
        public static string FormatDate2(System.DateTime dateValue, string formatString)
        {
            
            CultureInfo currentThreadCulture = null;
            string dateString = string.Empty;
            string realFormatString = formatString;
            bool convertToJapaneseDateFormat = false;

            try
            {

                if (dateValue <= DateTime.MinValue || dateValue >= DateTime.MaxValue)
                {
                    return string.Empty;
                }

                currentThreadCulture = Thread.CurrentThread.CurrentCulture;

                if (formatString.IndexOf("g") >= 0)
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("ja-jp");
                    Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = new JapaneseCalendar();
                    realFormatString = formatString.Replace("e", "y");
                    convertToJapaneseDateFormat = true;
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                }

                dateString = dateValue.ToString(realFormatString);

                if (convertToJapaneseDateFormat)
                {


                    foreach (string key in DateUtility.JapaneseYearNamePair().Keys)
                    {
                        dateString = dateString.Replace(key + "0", key);
                    }

                    if (formatString.IndexOf("gge") < 0 && formatString.IndexOf("ge") >= 0)
                    {

                        foreach (string key in DateUtility.JapaneseYearNamePair().Keys)
                        {
                            dateString = dateString.Replace(key, (string)(DateUtility.JapaneseYearNamePair()[key]));
                        }

                    }

                    if (formatString.IndexOf("年") > 0 && (new JapaneseCalendar()).GetYear(dateValue) == 1)
                    {
                        dateString = dateString.Replace("1年", "元年");
                    }


                }

                return dateString;
            }

            catch (Exception)
            {

                return string.Empty;
            }

            finally
            {

                if ((currentThreadCulture != null))
                {
                    Thread.CurrentThread.CurrentCulture = currentThreadCulture;
                }

            }

        }


        /// <summary>
        /// 从西历年度转到和历年度
        /// </summary>
        /// <param name="year">需要转换的西历年度</param>
        /// <returns>字符串类型的西历年度,比如"平成20",前两位是"平成"、"昭和"、"大正"、"明治"</returns>
        /// <remarks>平成元年1988、昭和元年1925、大正元年1911、明治元年1867</remarks>
        /// <history>
        /// [FuQi]   10/9/2008 1:21 PM    Created
        /// </history>
        public static string ConvertToJPYear(int year)
        {

            //平成
            int startPingCheng = 1988;

            //昭和
            int startZhaoHe = 1925;

            //大正
            int startDaZheng = 1911;

            //明治
            int startMingZhi = 1867;

            if (year > startPingCheng)
            {

                return "平成" + (year - startPingCheng).ToString();
            }

            else if (year > startZhaoHe)
            {

                return "昭和" + (year - startZhaoHe).ToString();
            }

            else if (year > startDaZheng)
            {

                return "大正" + (year - startDaZheng).ToString();
            }

            else if (year > startMingZhi)
            {

                return "明治" + (year - startMingZhi).ToString();

            }

            return string.Empty;

        }

        /// <summary>
        /// 从和历年转换到西历年
        /// </summary>
        /// <param name="year">需要转换的和历年度,int类型</param>
        /// <returns>返回西历年度，int类型</returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   10/9/2008 1:32 PM    Created
        /// </history>
        public static int ConvertToENYear(int year)
        {
            return year;
        }

        /// <summary>
        /// 判断传入的年份是不是闰年
        /// </summary>
        /// <param name="year">西历的4位年度，比如2008</param>
        /// <returns>
        /// 	<c>true</c> if [is leap year] [the specified year]; otherwise, <c>false</c>.
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   11/4/2008 11:34 AM    Created
        /// </history>
        public static bool IsLeapYear(int year)
        {
            return DateTime.IsLeapYear(year);
        }

    }
}
